const mongoose = require("mongoose")

const Schema = mongoose.Schema
const {Mixed,ObjectId} = Schema.Types

const CategoryShema = new Schema({
    name:{
        unique:true,
        type:String,
    },
    movies:[{
        type:ObjectId,
        ref:'Movie'
    }],
    meta:{
        createdAt:{
            type:Date,
            default:Date.now(),
        },
        upDatedAt:{
            type:Date,
            default:Date.now(),
        }
    }
})

CategoryShema.pre('save',function(next) {
    if(this.isNew){
        this.meta.createdAt = this.meta.upDatedAt = Date.now()
    }else{
        this.meta.upDatedAt = Data.now()
    }
    next()
})

mongoose.model('Category',CategoryShema)